/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2022-06-19 17:11:55
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2022-10-06 16:59:59
 * @FilePath: /imx6ull-app/09_jiffy_timer/jiffytimerApp.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "fcntl.h"
#include "stdio.h"
#include "sys/ioctl.h"
#include "unistd.h"
#include "stdlib.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"

#define CLOSE_CMD       (_IO(0xEF,0x01))
#define OPEN_CMD        (_IO(0xEF,0x02))
#define SETPERIOD_CMD   (_IO(0xEF,0x03))

/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: 
* function: 启动定时器 
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
int main(int argc, char *argv[])
{
    int fd,retval;
    unsigned int cmd;
    char *filename;
    unsigned int arg;
    unsigned char str[100];

    if (argc != 2)
    {
        printf("Error argc\r\n");
        return -1;
    }
    
    filename = argv[1];

    fd = open(filename,O_RDWR);
    if (fd < 0)
    {
        printf("Open failed\r\n");
        return -1;
    }
    
    while (1)
    {
        printf("Input CMD:\r\n\
                1:CLOSE TIMER\r\n\
                2:OPEN TIMER\r\n\
                3:RESET Period\r\n");

        retval = scanf("%d",&cmd);

        if (retval != 1)
        {
            fgets(str,sizeof(str),stdin);
        }

        switch (cmd)
        {
            case 1:cmd = CLOSE_CMD;     break;
            case 2:cmd = OPEN_CMD;      break;
            case 3:
                cmd = SETPERIOD_CMD;
                printf("Input Period:\r\n");
                retval = scanf("%d",&arg);
                if (retval != 1)
                {
                    fgets(str,sizeof(str),stdin);
                }
                break;
            default:break;
        }
        ioctl(fd,cmd,arg);
    }
    
    close(fd);

    return 0;
}
